package com.caifu.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.caifu.pojo.CDRecord;
import com.caifu.util.ImageUpload;
import com.caifu.mapper.CDRecordMapper;
import com.caifu.mapper.DeviceMapper;
import com.caifu.pojo.CDevice;
import com.caifu.service.ActiveMqService;
import com.caifu.util.Utils;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Encoder;

import javax.annotation.Resource;
import javax.jms.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AvtiveMqServiceImpl implements ActiveMqService {

    @Resource
    private DeviceMapper deviceMapper;
    @Resource
    private CDRecordMapper cdRecordMapper;

    @Override
    public Map<String, Object> addMqMessage(String deviceId) throws Exception {
        Map<String, Object> map = new HashMap<>();
        try {

            JSONObject json = new JSONObject();
            CDevice device = deviceMapper.selectById(Integer.valueOf(deviceId));

            Map creMap = new HashMap();
            creMap.put("deviceId", deviceId);
            creMap.put("minNum", 0);
            creMap.put("maxNum", 20);
            String bTemp = "";
//            List<Map<String, Object>> list1 = new ArrayList<>();
            List<Map<String, Object>> list = cdRecordMapper.listCDRecord(creMap);
            for (Map<String, Object> objectMap : list) {
                if(objectMap.get("sex") != null && !"".equals(objectMap.get("sex"))){
                    if("1".equals(objectMap.get("sex"))){
                        objectMap.put("sexName", "男");
                    } else if("2".equals(objectMap.get("sex"))){
                        objectMap.put("sexName", "女");
                    }
                }

//                Map reMap = new HashMap();
//                reMap.put("id",objectMap.get("id"));
//                reMap.put("userNo",objectMap.get("userNo"));
//                list1.add(reMap);

                if(objectMap.get("photo") != null && !"".equals(objectMap.get("photo"))){
                    byte[] bates = ImageUpload.image2Bytes(objectMap.get("photo").toString());
                    if(bates != null){
                        BASE64Encoder ecoder = new BASE64Encoder();
                        bTemp = ecoder.encode(bates);
                        bTemp = bTemp.replaceAll("[\\s*\t\n\r]", "");
                        objectMap.put("facePhoto", bTemp);
                    } else {
                        objectMap.put("facePhoto", "");
                    }
                }
            }
//            JSONObject object = new JSONObject();
//            object.put("list1",list1);
//            object.put("deviceUid", device.getDeviceUid());
//            System.out.println(object);
            if(list != null && list.size() >0){
                json.put("deviceUid", device.getDeviceUid());
                json.put("list", list);
//                System.out.println(json);

                //1.创建ConnectiongFactory,绑定地址
                ConnectionFactory factory=new ActiveMQConnectionFactory(Utils.ACTIVEMQ_URL);
                //2.创建Connection
                Connection connection= factory.createConnection();
                //3.启动连接
                connection.start();
                //4.创建会话
                Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                //5.创建一个目标 (主题类型)
                Destination destination=session.createTopic("door_order");
                //6.创建一个生产者
                MessageProducer producer=session.createProducer(destination);
                //7.创建消息
                TextMessage textMessage=session.createTextMessage(json.toString());
                //8.发送消息
                producer.send(textMessage);
                //9. 关闭连接
                connection.close();
                map.put("code", 0);
                map.put("msg", "推送成功");
                int id = 0;
                for (Map<String, Object> objectMap : list) {
                    id = Integer.valueOf(objectMap.get("id").toString());
                    CDRecord record = cdRecordMapper.selectById(id);
                    record.setStatus("1");
                    cdRecordMapper.updateById(record);
                }
            } else {
                map.put("code", 1);
                map.put("msg", "未查询到需下发人员");
            }


        } catch (Exception e){
            e.printStackTrace();
            map.put("code", 9);
            map.put("msg", "推送失败");
        }
        return map;
    }
}
